<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：tudougeaini
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open('','login');



switch ($mudi){
	case 'add':
		$menuFileID = 93;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'rev':
		$menuFileID = 94;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'del':
		$menuFileID = 95;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		del();
		break;

	case 'loadFieldInfo':
		$menuFileID = 92;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		LoadFieldInfo();
		break;

	case 'makeHtml':
		$menuFileID = 92;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		MakeHtml();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 添加与修改
function AddOrRev(){
	global $DB,$MB,$mudi,$menuFileID,$menuTreeID;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');

	$htmlNameOld	= OT::PostStr('htmlNameOld');
	$oldFatID		= OT::PostInt('oldFatID');

	$fatID			= OT::PostInt('fatID');
		if ($fatID > 0){ $level=2; }else{ $level=1; }
	$theme			= OT::PostReplaceStr('theme','html');
	$themeB			= OT::PostInt('themeB');
	$themeColor		= OT::PostStr('themeColor');
	$themeStyle		= '';
		if ($themeB == 1){ $themeStyle .= 'font-weight:bold;'; }
		if ($themeColor != ''){ $themeStyle .= 'color:'. $themeColor .';'; }
	$mode			= OT::PostStr('mode');
	$webKey			= OT::PostStr('webKey');
	$webDesc		= Str::RegExp(OT::PostStr('webDesc'),'html');
	$template		= OT::PostStr('template');
	$templateWap	= OT::PostStr('templateWap');
	$isHome			= OT::PostInt('isHome');
	$homeColor		= OT::PostStr('homeColor');
	$homeNum		= OT::PostInt('homeNum');
		if ($homeNum <= 0){ $homeNum=10; }
	$isHomeWap		= OT::PostInt('isHomeWap');
	$homeWapNum		= OT::PostInt('homeWapNum');
		if ($homeWapNum <= 0){ $homeWapNum=10; }
	$fieldNum		= OT::PostInt('fieldNum');
	$fieldStr		= '';
		for ($i=1; $i<=$fieldNum; $i++){
			$fieldStr .= OT::PostInt('fieldJud'. $i) .'|'. OT::PostStr('fieldName'. $i) .'|'. OT::PostStr('fieldColor'. $i) .'|'. OT::PostInt('fieldHome'. $i) .'|'. OT::PostInt('fieldRank'. $i) .'|'. OT::PostStr('fieldWidth'. $i) .'|'. OT::PostStr('fieldAlign'. $i) .'|<arr>';
		}
	$priceType		= OT::PostStr('priceType');
	$htmlName		= OT::PostStr('htmlName');
	$tixing			= OT::PostStr('tixing');
	$note			= OT::PostStr('note');
	$rank			= OT::PostInt('rank');
	$state			= OT::PostInt('state');
	$wapState		= OT::PostInt('wapState');
	
	if ($theme == ''){
		JS::AlertBackEnd('表单内容接收不全');
	}

	$record=array();
	$record['IPT_revTime']		= TimeDate::Get();
	$record['IPT_level']		= $level;
	$record['IPT_fatID']		= $fatID;
	$record['IPT_theme']		= $theme;
	$record['IPT_mode']			= $mode;
	$record['IPT_webKey']		= $webKey;
	$record['IPT_webDesc']		= $webDesc;
	$record['IPT_isHome']		= $isHome;
	$record['IPT_homeColor']	= $homeColor;
	$record['IPT_homeNum']		= $homeNum;
	$record['IPT_isHomeWap']	= $isHomeWap;
	$record['IPT_homeWapNum']	= $homeWapNum;
	$record['IPT_fieldNum']		= $fieldNum;
	$record['IPT_fieldStr']		= $fieldStr;
	$record['IPT_priceType']	= $priceType;
	$record['IPT_htmlName']		= $htmlName;
	$record['IPT_tixing']		= $tixing;
	$record['IPT_note']			= $note;
	$record['IPT_rank']			= $rank;
	$record['IPT_state']		= $state;
	$record['IPT_wapState']		= $wapState;
	if (AppBase::Jud()){
		$record['IPT_themeStyle']	= $themeStyle;
		$record['IPT_template']		= $template;
	}
	if (AppWap::Jud()){
		$record['IPT_templateWap']	= $templateWap;
	}

	$dealrec=$DB->query('select * from '. OT_dbPref .'idcProType where IPT_ID='. $dataID);
		if (! $row = $dealrec->fetch()){
			$alertMode='添加';
			$record['IPT_time']	= TimeDate::Get();

			$judRes = $DB->InsertParam('idcProType',$record);
		}else{
			$alertMode='修改';
			if ($oldFatID==0 && $fatID>0){
				$checkexe=$DB->query('select IPT_ID from '. OT_dbPref .'idcProType where IPT_fatID='. $dataID);
				if ($checkexe->fetch()){
					JS::AlertBackEnd('该栏目下存在子栏目，请选转移或删除子栏目，再选择非顶级分类。');
				}
				unset($checkexe);
			}
			if ($oldFatID != $fatID){
				if ($oldFatID == 0){ $oldLevel=1; }else{ $oldLevel=2; }
				if ($fatID == 0){
					$setStr="IPD_typeStr=',". $dataID .",',IPD_type1ID=". $dataID .",IPD_type2ID=0";
				}else{
					$setStr="IPD_typeStr=',". $fatID .",". $dataID .",',IPD_type1ID=". $fatID .",IPD_type2ID=". $dataID ."";
				}
				$DB->query('update '. OT_dbPref .'idcProData set '. $setStr .' where IPD_type'. $oldLevel .'ID='. $dataID);
			}
			$judRes = $DB->UpdateParam('idcProType',$record,'IPT_ID='. $dataID);
		}
	unset($dealrec);

	if ($alertMode == '添加'){
		if ($dataID == 0){ $dataID=$DB->GetOne('select max(IPT_ID) from '. OT_dbPref .'idcProType'); }
		$backURL = 'idcProType.php?mudi=add&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&dataID='. $dataID .'';
	}

	if ($alertMode == '修改' && $htmlNameOld != $htmlName && $htmlName != ''){
		if ($DB->GetOne('select count(IPT_ID) from '. OT_dbPref .'idcProType where IPT_fatID='. $dataID) == 0){
			$DB->query("update ". OT_dbPref ."idcProData set IPD_idcProTypeDir='". $DB->ForStr($htmlName) ."/' where IPD_typeStr like '%,". $dataID .",%'");
		}
	}

	if ($judRes == true){
		$alertRes = '成功';
	}else{
		$alertRes = '失败';
	}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】'. $alertMode . $alertRes .'！',
		));

	JS::AlertHrefEnd($alertMode . $alertRes .'！', $backURL);
}



// 删除
function del(){
	global $DB,$skin,$mudi,$menuFileID,$menuTreeID,$systemArr;

	$dataType	= OT::GetStr('dataType');
	$dataTypeCN	= OT::GetStr('dataTypeCN');
	$dataID		= OT::GetInt('dataID');
	$theme		= OT::GetStr('theme');

	if ($dataID<=0){
		JS::AlertEnd('指定ID错误！');
	}

	$IPT_htmlName = '';
	$checkexe = $DB->query('select IPT_ID,IPT_level,IPT_fatID,IPT_htmlName from '. OT_dbPref .'idcProType where IPT_ID='. $dataID);
	if (! $row = $checkexe->fetch()){
		JS::AlertEnd('该栏目不存在');
	}else{
		if ($row['IPT_level'] == 1 || $row['IPT_fatID'] == 0){
			$checkexe2 = $DB->query('select IPT_ID from '. OT_dbPref .'idcProType where IPT_fatID='. $dataID);
			if ($checkexe2->fetch()){
				JS::AlertEnd('该类别下存在子类别，请先删除子类别。');
			}
			unset($checkexe2);
		}
		$IPT_htmlName = $row['IPT_htmlName'];
	}
	unset($checkexe);

	$chkNum = $DB->GetOne("select count(IPD_ID) from ". OT_dbPref ."idcProData where IPD_typeStr like '%,". $dataID .",%'");
		if ($chkNum > 0){
			JS::AlertEnd('【IDC商品管理】有 '. $chkNum .' 个商品占用该类别，请先转移或删除被占用的商品。');
		}

	$judRes = $DB->query('delete from '. OT_dbPref .'idcProType where IPT_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));

	echo('
	<script language="javascript" type="text/javascript">
	parent.$id("data'. $dataID .'").style.display="none";
	</script>
	');
}



// 读取
function LoadFieldInfo(){
	global $DB;
	$dataID		= OT::GetInt('dataID');

	$row = $DB->GetRow('select IPT_fieldNum,IPT_fieldStr,IPT_priceType from '. OT_dbPref .'idcProType where IPT_ID='. $dataID);

	$fieldArr = explode('<arr>', $row['IPT_fieldStr'] .'<arr><arr><arr><arr><arr><arr><arr><arr><arr><arr><arr><arr>');
	$fieldNum = count(array_filter($fieldArr));
	$fieldList = '';
	for ($i=1; $i<=12; $i++){
		$itemArr = explode('|', $fieldArr[$i-1] .'|||||');
		$fieldList .= '
			$id("fieldJud'. $i .'").checked = '. ($itemArr[0]==1 ? 'true' : 'false') .';
			$id("fieldName'. $i .'").value = "'. $itemArr[1] .'";
			$id("fieldColor'. $i .'").value = "'. $itemArr[2] .'";
			$id("fieldHome'. $i .'").value = "'. ($itemArr[3]==1 ? 'true' : 'false') .'";
			$id("fieldRank'. $i .'").value = "'. $itemArr[4] .'";
			';
	}

	echo('
	$id("fieldNum").value = "'. $row['IPT_fieldNum'] .'";
	$id("priceType").value = "'. $row['IPT_priceType'] .'";
	CheckField();
	'. $fieldList);
}



function MakeHtml(){
	global $DB,$systemArr;

	$mode			= OT::GetStr('mode');
		if ($mode == 'wap'){ $dir='wap/'; }else{ $dir=''; $mode='pc'; }
	$dataID			= OT::GetInt('dataID');

	if ($dataID <= 0){
		JS::AlertEnd('指定ID错误.');
	}

	$infoexe = $DB->query('select * from '. OT_dbPref .'idcProType where IPT_ID='. $dataID);
		if (! $row = $infoexe->fetch()){
			JS::AlertEnd('指定的IDC商品类别不存在('. $dataID .').');
		}
		if ($row['IPT_state'] == 0){
			JS::AlertEnd('该IDC商品类别状态已关闭.');
		}
		if ($mode == 'wap'){
			$filePath = '../wap/idcPro.php?dataID='. $row['IPT_ID'] .'&isHtml=1';
		}else{
			$filePath = '../idcPro.php?dataID='. $row['IPT_ID'] .'&isHtml=1';
			$mode = 'pc';
		}

	echo('
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<script language="javascript" type="text/javascript">
	try { 
		parent.document.getElementById("DiyRateBox'. $dataID .'").style.display="";
		parent.WindowHeight(0);
	 }catch (e){}
	</script>

	<form id="makeForm" name="makeForm" method="post" action="'. $filePath .'">
	<input type="hidden" name="id" value="'. $dataID .'">
	<input type="hidden" name="rnd" value="99999">
	正在跳转中，请稍等……  如没自动跳转，<input type="submit" name="subBtn" value="请点此继续登录...........">
	</form>
	<script language="javascript" type="text/javascript">
	document.getElementById("makeForm").submit();
	</script>
	');

	unset($infoexe);
}

?>